package com.irdstudio.tdp.executor.core.dao;

import com.irdstudio.tdp.executor.core.dao.domain.MigrateDictOption;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 新系统字典选项信息类-[表名: 新系统字典选项信息(migrate_dict_option)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-13
 */
public class MigrateDictOptionDao {

	/* 连接对象 */
	Connection conn = null;	

	public MigrateDictOptionDao(Connection conn){
		this.conn = conn;
	}


    /**
	* 新增一行
	* @return int
	*/
    public int insertMigrateDictOption(MigrateDictOption migrateDictOption) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("insert into migrate_dict_option ( "
                + "dict_id,option_code,option_name"
                + ") values ("+ "?,?,?" +")"
                );
            ps.setObject(1, migrateDictOption.getDictId());
            ps.setObject(2, migrateDictOption.getOptionCode());
            ps.setObject(3, migrateDictOption.getOptionName());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("insert MigrateDictOption is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键删除一行记录
	* @return int
	*/
    public int deleteByPk(MigrateDictOption migrateDictOption) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("delete from migrate_dict_option where 1=1"
                + " AND dict_id = ? "
                + " AND option_code = ? "
                );
            ps.setObject(1, migrateDictOption.getDictId());
            ps.setObject(2, migrateDictOption.getOptionCode());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("delete MigrateDictOption is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键更新一行记录
	* @return int
	*/
    public int updateByPk(MigrateDictOption migrateDictOption) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("update migrate_dict_option set "
                + " dict_id = ? ,"
                + " option_code = ? ,"
                + " option_name = ? "
                + " where 1=1"
                + " AND dict_id = ? "
                + " AND option_code = ? "
                );
            ps.setObject(1, migrateDictOption.getDictId());
            ps.setObject(2, migrateDictOption.getOptionCode());
            ps.setObject(3, migrateDictOption.getOptionName());
            ps.setObject(4, migrateDictOption.getDictId());
            ps.setObject(4, migrateDictOption.getOptionCode());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("update MigrateDictOption is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }

	}

    /**
	* 根据主键查询一行记录
	* @return int
	*/
    public MigrateDictOption queryByPk(MigrateDictOption migrateDictOption) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        MigrateDictOption result = null;
        try {
            ps = conn.prepareStatement("select dict_id,option_code,option_namefrom migrate_dict_option where 1=1 "
                + " AND dict_id = ? "
                + " AND option_code = ? "
                );
            ps.setObject(1, migrateDictOption.getDictId());
            ps.setObject(2, migrateDictOption.getOptionCode());
            rs = ps.executeQuery();
            if (rs.next()) {
				result = new MigrateDictOption();
				result.setDictId(rs.getString("dict_id"));
				result.setOptionCode(rs.getString("option_code"));
				result.setOptionName(rs.getString("option_name"));
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateDictOption is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	* 根据 migrateDictOption 参数列表查询 不分页
	* @return int
	*/
    public List<MigrateDictOption> queryAll(MigrateDictOption migrateDictOption) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<MigrateDictOption> result = new ArrayList<>();
        try {
            String sql = "select dict_id,option_code,option_namefrom migrate_dict_option where 1=1 ";
                if (migrateDictOption.getDictId() != null) {
                    sql = sql + " AND dict_id =  '" + migrateDictOption.getDictId() + "'";
                }
                if (migrateDictOption.getOptionCode() != null) {
                    sql = sql + " AND option_code =  '" + migrateDictOption.getOptionCode() + "'";
                }
                if (migrateDictOption.getOptionName() != null) {
                    sql = sql + " AND option_name =  '" + migrateDictOption.getOptionName() + "'";
                }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
				MigrateDictOption obj = new MigrateDictOption();
				obj.setDictId(rs.getString("dict_id"));
				obj.setOptionCode(rs.getString("option_code"));
				obj.setOptionName(rs.getString("option_name"));
                result.add(obj);
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateDictOption is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
    protected void close(ResultSet theRs, Statement theStmt, PreparedStatement thePs) {
        try {
            if (theRs != null)
                theRs.close();
            if (theStmt != null)
                theStmt.close();
            if (thePs != null)
                thePs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
